package com.hand.demo.contoller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping(value = "book2")
public class BookRestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/{id}/get")
    public ResponseEntity get(@PathVariable(name = "id",required = true)String id){
        if(id.isEmpty()){
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        try {
            Map novel = restTemplate.getForObject("http://127.0.0.1:9200/book/novel/1{id}",
                    Map.class, id);

            return ResponseEntity.ok(novel);
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return null;

    }


}
